#    Copyright 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import COOPGIS_Ajax, COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update
import COOPGIS_FormMisc, COOPGIS_FormGeography, COOPGIS_GPS
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
import _mysql as mysql
from models import *
from django import forms
import django


def GPSTrace(request):
    ApplicationName=djangoproject.settings.COOPGISGPSTraceApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName, DefaultMap)
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    COOPGIS_FormGeography.ProcessGPSTraceForm(form)
    form=COOPGIS_FormGeography.PopulateGPSTraceForm(form)
    if(djangoproject.models.geography.GPSTrace.objects.all()):
        OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(point=djangoproject.models.geography.GPSTrace.objects.all().order_by('Order')[0].Point)
    else:
        OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom()
    MapFileName, LayerName =COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    try:
        MinimumOrder=djangoproject.models.geography.GPSTrace.objects.all().order_by('Order')[0].Order
    except:
        MinimumOrder='None'
    try:
        MaximumOrder=djangoproject.models.geography.GPSTrace.objects.all().order_by('-Order')[0].Order
    except:
        MaximumOrder='None'
    NumberOfPoints=djangoproject.models.geography.GPSTrace.objects.all().count()

    t = loader.get_template('/opt/coopgis/djangoproject/templates/gpstrace.html')
    c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                 'MapFileName': MapFileName, 'LayerName':LayerName,
                 'MinimumOrder': MinimumOrder, 'MaximumOrder': MaximumOrder,
                 'NumberOfPoints': NumberOfPoints})
    return HttpResponse(t.render(c))

def GPSTraceInProgress(request):
    ApplicationName=djangoproject.settings.COOPGISGPSTraceApplicationName
    GPSPoint, Quality=COOPGIS_GPS.GetGPSPoint(djangoproject.settings.COOPGISGPSFileName)
    if(GPSPoint):
        if(djangoproject.models.geography.GPSTrace.objects.all()):
            Order=djangoproject.models.geography.GPSTrace.objects.all().order_by('-Order')[0].Order + 5
        else:
            Order=5
        gPSTrace=djangoproject.models.geography.GPSTrace()
        gPSTrace.Order=Order
        gPSTrace.Point=GPSPoint
        gPSTrace.save()
    DateTime=datetime.datetime.now()
    t = loader.get_template('/opt/coopgis/djangoproject/templates/gpstraceinprogress.html')
    c = Context({'GPSPoint':  GPSPoint, 'Quality':Quality, 'DateTime':DateTime})
    return HttpResponse(t.render(c))


